const blue = require('./blue.js')
var   adapter =  new blue.BLEAdapter()
//var   connection= new blue.BLEConnection()
//var   service =new blue.BLEService()
const app = getApp()
const request=require('../../request.js')
var command=new request.Command()
const tools=require('../../tools')


Page({
  data: {
    devices:[],
    name:'',
    deviceId: '',
    sendStatus: 'pending',
    deviceList: [],
    deviceIndex: 0,
    connected: false
  },

  async onLoad() {
    app.index=this   
    let openAdapter=await adapter.openBLEAdapter()    
    let result = await adapter.startScan();
    let found =await adapter.onBluetoothDeviceFound() 
    setTimeout(() => {
      command.callResolve();
    }, 3000);
    let sleep=await command.sleep()
    let stopScan=await adapter.stopScan()
    let devices= adapter.peekDeviceId()
    let keys = Object.keys(devices)
    let deviceId = keys[0]
    console.log(devices)
    this.setData({
      devices,
      deviceId
    })
    console.log(deviceId) 
  },
   createBLEConnection(){
    let deviceId=this.data.deviceId
    adapter.connect(deviceId)
    .then(() => {
      console.log('success')
    })
    .catch((err)=>{
      console.log('catch error',err)
    })
  },
  
   onUnload() {
    // 关闭适配器
    adapter.closeBluetoothAdapter()
    this.devices=[]
  }
})